<template>
  <div class="login">
    <div style="width: 60%;padding: 5% 10%;height: 100%;">
      <div style="text-align: center;position: relative;">
        <img src="../assets/images/circle-background.png" alt="" style="width: 85%;position: relative;z-index: 1;">
        <img src="../assets/images/search.png" alt="" style="width: 30%;position: absolute;right: 25%;top:0;z-index: 100;animation:animY-Search 3s linear infinite alternate;">
        <img src="../assets/images/edit.png" alt="" style="width: 45%;position: absolute;left: 0;top: 30%;z-index: 100;animation:animY-Edit 3s linear infinite alternate;">
        <img src="../assets/images/computer.png" alt="" style="width: 90%;position: absolute;top: 45%;left: 20%;z-index: 1;">
        <img src="../assets/images/repair.png" alt="" style="width: 25%;position: absolute;top: 70%;left: 25%;z-index: 100;animation:animY-Repair 3s linear infinite alternate;">
        <img src="../assets/images/cloud.png" alt="" style="width: 65%;position: absolute;top: 20%;right: -15%;z-index: 100;animation:animY-Cloud 3s linear infinite alternate;">
        <div style="width: 100%;top: 30%;position: absolute;height: 60%;rotate: 30deg;">
          <img src="../assets/images/circle.png" alt="" style="rotate: -30deg;width: 100%;">
          <img src="../assets/images/point.png" alt="" class="point">
          <img src="../assets/images/point.png" alt="" class="point1">
          <img src="../assets/images/point.png" alt="" class="point2">
        </div>
        <div style="width: 100%;top: 35%;left: 5%;position: absolute;height: 60%;rotate: -10deg;">
          <img src="../assets/images/circle.png" alt="" style="rotate: -30deg;width: 100%;">
          <img src="../assets/images/point.png" alt="" class="point">
          <img src="../assets/images/point.png" alt="" class="point1">
          <img src="../assets/images/point.png" alt="" class="point2">
        </div>
      </div>
      <div style="text-align: center;">
        <img src="../assets/images/base.png" alt="" style="width: 85%;margin-top: 2%;">
      </div>
    </div>
    <div style="width: 40%;height: 100%;padding-top: 6%;">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-width="20%">
        <img src="../assets/logo/logo.png" alt="" style="width: 30%">
        <img src="../assets/images/title.png" alt=""
          style="width:60%;display: block;margin-bottom: 10%;margin-top: 2%;">
        <el-form-item prop="username" class="input-background" label="账号">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" clearable placeholder="账号">
          </el-input>
        </el-form-item>
        <div class="box-background"></div>
        <el-form-item prop="password" label="密码" class="input-background">
          <el-input v-model="loginForm.password" type="password" auto-complete="off" show-password placeholder="密码"
            @keyup.enter.native="handleLogin">
            <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
          </el-input>
        </el-form-item>
        <div class="box-background"></div>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;font-size: 0.8vh;">记住密码</el-checkbox>
        <el-checkbox v-model="loginForm.aggrement" style="margin:0px 0px 25px 0px;font-size: 0.8vh;display: block;">同意《服务协议》和《隐私协议》</el-checkbox>
        <el-form-item style="width:100%;">
          <div class="button-background"> 
          </div> 
          <div class="button-text" v-if="!loading" @click="handleLogin">登 录</div>
          <div class="button-text" v-else>登 录 中...</div>
         
          <!-- <el-button :loading="loading" size="medium" type="primary" style="width:50%;" class="button-background"
            >
          </el-button> -->
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 国创智能矿山装备研究院(洛阳)有限公司</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
        aggrement:false
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            // this.$router.push({ path: this.redirect || "/" }).catch(() => { });
            console.log(this.redirect)
            this.$router.push({ path:"/" }).catch(() => { });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.png");
  background-size: cover;
  overflow-y: hidden;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  background: transparent;
  width: 100%;
  height: 100%;
  padding: 25px 25px 5px 25px;

  .el-form-item {
    height: 8%;
    width: 90%;
    margin-bottom: 0;
    clip-path: polygon(0 0, 100% 0, 90% 100%, 0 100%);
    position: relative;
    z-index: 100;
    border-radius: 4px;

    .el-form-item__label {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
      letter-spacing: 0.5rem;
      font-size: 2vh;
    }

    .el-form-item__content {
      height: 100%;

      .el-input {
        height: 100%;
        width: 85%;
        display: flex;
        justify-content: center;
        align-items: center;

        input {
          height: 50%;
          background-color: transparent;
          border-color: transparent;
          border-left: solid 2px #001d67;
          border-radius: 0;
          font-size: 1.5vh;
        }
      }
    }
  }
  .el-checkbox__label,
  .el-checkbox__input.is-checked + .el-checkbox__label{
    color: #007DED;
    letter-spacing: 0.5rem;
    font-size: 1.6vh;
  }
  .el-checkbox__inner,
  .el-checkbox__input.is-checked .el-checkbox__inner{
    border-color: #007DED;
    background-color: transparent;
    border-width: 1.5px;
    width: 1.8vh;
    height: 1.8vh;
  }
  .el-checkbox__inner::after{
    border-color: #007DED;
    border-width: 1.5px;
    width: 0.4vh;
    height: 0.9vh;
  }
  .el-checkbox__input{
    vertical-align: sub;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

.input-background {
  background: linear-gradient(90deg, #004B8E 1.04%, #002F58 41.54%, #000F1F 100%);
}

.box-background {
  background: #005C91;
  box-shadow: 0px 4px 20px #003772;
  filter: blur(7.5px);
  position: relative;
  top: -1%;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2%;
  width: 80%;
  z-index: 1;
  margin-bottom: 10%;
}

.button-background {
  width: 50%;
  height: 90%;
  background: linear-gradient(180deg, #026BC9 0%, #00549F 51.5%, #002F5D 100%);
  opacity: 0.5;
  border-radius: 5px;
  filter: drop-shadow(0px 4px 20px #003772);
}
.button-text{
  width: 50%;
  height: 90%;
  position: absolute;top: 0;left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  letter-spacing: 0.5rem;
  font-size: 2.5vh;font-weight: bold;background: linear-gradient(180deg, #FFFFFF 30.21%, #001A5C 123.96%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  cursor: pointer;
}
.point{
  position: absolute;
  width: 3%;
  animation: animX 10s cubic-bezier(0.36, 0, 0.64, 1) -5s infinite alternate, animY 10s cubic-bezier(0.36, 0, 0.64, 1) 0s infinite alternate;
}
.point1{
  position: absolute;
  width: 3%;
  animation: animX 10s cubic-bezier(0.36, 0, 0.64, 1) -10s infinite alternate, animY 10s cubic-bezier(0.36, 0, 0.64, 1) -5s infinite alternate;
}
.point2{
  position: absolute;
  width: 3%;
  animation: animX 10s cubic-bezier(0.36, 0, 0.64, 1) -15s infinite alternate, animY 10s cubic-bezier(0.36, 0, 0.64, 1) -10s infinite alternate;
}
@keyframes animX{
  from {
    left: -7%;
  }
  to {
    left: 104%;
  }
}
@keyframes animY{
  from {
    top: 16.5%;
  }
  to {
    top: 121%;
  }
}

@keyframes animY-Edit{
  from {
    top: 30%;
  }
  to {
    top: 25%;
  }
}

@keyframes animY-Computer{
  from {
    top: 45%;
  }
  to {
    top: 40%;
  }
}

@keyframes animY-Cloud{
  from {
    top: 20%;
  }
  to {
    top: 15%;
  }
}
@keyframes animY-Search{
  from {
    top: 0%;
  }
  to {
    top: -5%;
  }
}
@keyframes animY-Repair{
  from {
    top: 70%;
  }
  to {
    top: 65%;
  }
}
</style>
